=========
Packaging
=========

How to package SieveManager for Debian_.

.. caution::
    The following commands require that the
    repository is your working directory.


Store the SieveManger version in `$vers` and package name in `$deb`:

.. code:: bash

    vers="$(python3 -c 'import sievemgr; print(sievemgr.__version__)')"
    deb="sievemgr_${vers:?}-0_all"


Create the staging directory tree:

.. code:: bash

    mkdir -p "${deb:?}/DEBIAN" "${deb:?}/usr/share/doc/sievemgr"


Create the package metadata:

.. code:: bash

    cat <<EOF >"${deb:?}/DEBIAN/control"
    Package: sievemgr
    Version: ${vers:?}
    Section: utils
    Priority: optional
    Architecture: all
    Depends: python3 (>=3.9)
    Maintainer: J. Doe <jdoe@example.com>
    Homepage: https://odkr.codeberg.page/sievemgr
    Description: Fully-featured ManageSieve command-line client and library
     SieveManager is a command-line client for uploading, downloading,
     and managing Sieve scripts using the ManageSieve protocol. It can
     also be used as a Python module.
    EOF

.. important::
    Consider adding the cryptography_ and dnspython_ Python
    modules as dependencies.


Create the copyright file:

.. code:: bash

    cat <<'EOF' >"${deb:?}/usr/share/doc/sievemgr/copyright"
    Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
    Source: https://odkr.codeberg.page/sievemgr
    Upstream-Name: SieveManager
    Upstream-Contact: Odin Kroeger
    Copyright: 2023 and 2024  Odin Kroeger
    License: GPL-3+

    Files: *
    Copyright: 2023 and 2024  Odin Kroeger
    License: GPL-3+
    EOF


Copy the change log:

.. code:: bash

    gzip -9n <NEWS.rst >"${deb:?}/usr/share/doc/sievemgr/changelog.gz"


Install SieveManager to the staging directory:

.. code:: bash

    make -e DESTDIR="${deb:?}" prefix=/usr install

.. tip::
    The :file:`makefile` conforms to the `GNU Coding standards`_ (chap. 7.2).


Debian's Python uses :file:`/usr/lib/python{X}/dist-packages`,
*not* :file:`/usr/lib/python{X}.{Y}/site-packages`:

.. code:: bash

    pythondir="$(ls -d "${deb:?}"/usr/lib/python*)"
    debiandir="${pythondir%.*}"
    mv "$pythondir" "$debiandir"
    mv "$debiandir/site-packages" "$debiandir/dist-packages"


Compress the manual pages:

.. code:: bash

    gzip -9n "${deb:?}/usr/share/man/man1/"*.1
    gzip -9n "${deb:?}/usr/share/man/man5/"*.5


Remove Python caches created by :command:`pip`:

.. code:: bash

    find "${deb:?}" -type d -name __pycache__ -exec rm -rf '{}' +


Create the Debian package:

.. code:: bash

    dpkg-deb --root-owner-group --build "${deb:?}"


Sign the package:

.. code:: bash

    debsigs --sign=maint "${deb:?}.deb"


.. _Debian: https://www.debian.org

.. _`GNU Coding standards`: https://www.gnu.org/prep/standards

.. _cryptography: https://cryptography.io

.. _dnspython: https://www.dnspython.org/
